package com.lsfb.signsys.view.BottomNavView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by zgscwjm on 2018/3/28.
 */

public class BadgeView extends View implements IBadge {

    /**
     * 简单模式
     */
    private final static int MODEL_SIMPLE = 0;

    /**
     * 显示数字
     */
    private final static int MODEL_COUNTER = 1;

    private final static int SIMPLE_SIZE = 10;
    /**
     * 背景色
     */
    private int mBackgroudColor;

    /**
     * 计数器
     */
    private int mCounter;

    private String mCounterStr;

    /**
     * 文字颜色
     */
    private int mTextColor;

    /**
     * 模式
     */
    private int mBadgeMole;


    private Paint mPaint;

    private Paint.FontMetrics mFontMetrics;

    public BadgeView(Context context) {
        this(context, null);
    }

    public BadgeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BadgeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mCounterStr = String.valueOf(mCounter);
        mTextColor = Color.WHITE;
        mBackgroudColor = Color.RED;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mBackgroudColor);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setTextSize(20);
        mFontMetrics = mPaint.getFontMetrics();

        //默认是隐藏
        this.setVisibility(GONE);

    }




    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth() / 2, getHeight() / 2);
        if (mBadgeMole == MODEL_COUNTER&&mCounter>0) {
            float textWidth = mPaint.measureText(mCounterStr);
            float textHeight = (mFontMetrics.descent - mFontMetrics.ascent);
            float textVC = textHeight / 2 + mFontMetrics.ascent;
            float textHC = textWidth / 2;

            //如果高大于宽
            if (textWidth < textHeight) {
                float size = (float) (textHeight/2 * 1.3);
                canvas.drawCircle(0, 0, size, mPaint);
            } else {
                float hsize = (float) (textHC * 1.3);
                float vsize = (float) (textHeight / 2 * 1.3);
                RectF rectF = new RectF(-hsize, -vsize, hsize, vsize);
                canvas.drawRoundRect(rectF, 300, 300, mPaint);
            }

            mPaint.setColor(mTextColor);
            canvas.drawText(mCounterStr, -textHC, -textVC, mPaint);
            mPaint.setColor(mBackgroudColor);

        } else {
            canvas.drawCircle(0, 0, SIMPLE_SIZE, mPaint);
        }
    }


    public int getBackgroudColor() {
        return mBackgroudColor;
    }

    public void setBackgroudColor(int BackgroudColor) {
        this.mBackgroudColor = BackgroudColor;
    }

    public int getCounter() {
        return mCounter;
    }

    public void setCounter(int Counter) {
        this.mCounter = Counter;
        mCounterStr = String.valueOf(mCounter);
        postInvalidate();
    }

    @Override
    public void BadgeVisibility(boolean visibility) {
        this.setVisibility(visibility?VISIBLE:GONE);
    }

    public int getBadgeMole() {
        return mBadgeMole;
    }

    public void setBadgeMole(int BadgeMole) {
        this.mBadgeMole = BadgeMole;
        postInvalidate();
    }

    public void incrCount(){
        mCounter++;
        mCounterStr = String.valueOf(mCounter);
        postInvalidate();
    }

    public void decrCount(){
        mCounter--;
        if (mCounter<0){
            mCounter = 0;
        }
        mCounterStr = String.valueOf(mCounter);
        postInvalidate();
    }



}
